import PouchDB from "pouchdb";
class DB {
  constructor(name, fetch) {
    this.currentDB = this.create(name, fetch);
    return this;
  }
  // 创建数据库
  create(name, fetch) {
    const pouchDB = fetch ? new PouchDB(name, fetch) : new PouchDB(name);
    return pouchDB;
  }
  // 删除数据库
  destroy(name) {
    return this.currentDB.destroy();
  }
  // 获取数据库基本信息
  info(name) {
    return this.currentDB.info();
  }
  // 监听数据库数据变化
  change(name, opts = {}) {
    return this.currentDB.changes(opts);
  }
  /**
   * 复制数据至其他数据库
   * @param {*} source 
   * @param {*} target 
   * @param {*} opts 
   * @returns 返回转移实例,支持回调.on("change"|"paused"|"active"|"denied"|"complete"|"error",callback),.cancel()方法取消复制
   * 数据同步
   * A.replicate.to(B);
     B.replicate.from(A);
   */
  replicate(source, target, opts = {}) {
    return PouchDB.replicate(source, target, opts);
  }
  /**
   * 同步数据库
   * @param {*} src
   * @param {*} target
   * @param {*} opts
   * @returns 返回转移实例,支持回调.on("change"|"paused"|"active"|"denied"|"complete"|"error",callback),.cancel()方法取消复制
   */
  sync(src, target, opts = {}) {
    return PouchDB.sync(source, target, opts);
  }
  /**
   * 创建文档
   * @param {*} doc {_id 必备字段}
   * 可以内联附件
   * "_attachments": {
        "att.txt": {
          "content_type": "text/plain",
          "data": "TGVnZW5kYXJ5IGhlYXJ0cywgdGVhciB1cyBhbGwgYXBhcnQKTWFrZS" +
                  "BvdXIgZW1vdGlvbnMgYmxlZWQsIGNyeWluZyBvdXQgaW4gbmVlZA=="
        }
      }
   */
  addDoc(doc) {
    return this.currentDB.put(doc);
  }
  /**
   * 更新文档
   * @param {Object} doc { ...其他属性,_rev: '必填项'}
   * 可以内联附件
   * "_attachments": {
        "a.txt": {
          "content_type": "text/plain",
          "data": new Blob(['Is there life on Mars?'], {type: 'text/plain'})
        },
        "b.txt": {
          "content_type": "text/plain",
          "data": new Blob(['Is there life on Mars?'], {type: 'text/plain'})
        }
      }
   */
  updateDoc(doc) {
    return this.currentDB.put(doc);
  }
  /**
   * 获取文档
   * @param {*} id
   * @returns {_id: '001',_rev: '3-552920d1ca372986fad7b996ce365f5d'}
   */
  getDoc(id, opts = {}) {
    return this.currentDB.get(id, opts);
  }
  /**
   * 删除文档
   * @param {*} id
   * @param {*} rev
   */
  deleteDoc(id, rev) {
    return this.currentDB.remove(id, rev);
  }
  /**
   * 创建批次
   * @param {Array} docs [{_id 必备字段}]
   */
  addBulkDocs(docs) {
    return this.currentDB.bulkDocs(docs);
  }
  /**
   * 更新批次
   * @param {Array} docs [{_id:'必填',_rev: '必填',...其他属性}]
   */
  updateBulkDocs(docs) {
    return this.currentDB.bulkDocs(docs);
  }
  // 获取批次
  getBulkDocs(
    opts = {
      include_docs: true
    }
  ) {
    return this.currentDB.allDocs(opts);
  }
  /**
   * 删除批次
   * @param {Array} docs [{_id:'必填',_rev: '必填',_deleted:true}]
   */
  deleteBulkDocs(docs) {
    return this.currentDB.bulkDocs(docs);
  }
  /**
   * 添加附件
   * @param {*} docId
   * @param {*} attachmentId a.txt
   * @param {*} attachment Blob|Buffer String会被当做base64
   * @param {*} type "text/plain"
   * @returns
   */
  addAttachment(docId, attachmentId, attachment, type) {
    return this.currentDB.putAttachment(docId, attachmentId, attachment, type);
  }
  // 检索附件
  getAttachment(docId, attachmentId, opts = {}) {
    return this.currentDB.getAttachment(docId, attachmentId, opts);
  }
  // 删除附件
  deleteAttachment(docId, attachmentId, rev) {
    return this.currentDB.getAttachment(docId, attachmentId, rev);
  }
}
export default DB;
